<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BScreenPersonalMessage
 *
 * @author ID59C
 */
class BScreenRequestJoinSpace extends BUIScreen {

    private $opRequests = null;

    public function onRender($data = null) {
        //parent::render($data);

        $this->addRenderView('processJoinSpaceRequest', 'processJoinSpaceRequest');
        //$this->addRenderView('messageItemView', 'messageItemView2');
        //$this->startRender();
    }

    public function getJoinSpaceRequests() {
        $ocJoinSpaceMember = new BOCJoinSpace();

        $this->opRequests = $ocJoinSpaceMember->getOperationRequests();

        return $this->opRequests;
    }

    public function addSpaceMember($requestId, $action) {
        foreach ($this->opRequests as $rqId => $opReq) {
            if ($rqId == $requestId) {
                if ($action == 'accept') {
                    $ocJoinSpace = new BOCJoinSpace();
                    $operation = array('name' => $opReq->operationName, 'data' => $opReq->data);
                    $receiver = $opReq->receiver;
                    $sender = $opReq->sender;
                    $result = $ocJoinSpace->processRequest($requestId, $operation, $receiver, $sender);

                    if ($result !== FALSE) {
                        $ocSendMessage = new BOCSpaceMessage();
                        $operation = array('name' => BOCSpaceMessage::Operation_SendMessage, 'data' => array('message' => 'join to Space request is accepted'));
                        $ocSendMessage->sendOperationRequest($operation, $opReq->sender);
                    }
                }
                if ($action == 'reject') {
                    $ocJoinSpace = new BOCJoinSpace();
                    $ocJoinSpace->updateOperationRequestData($requestId, array('status' => 'rejected'));
                    $ocSendMessage = new BOCSpaceMessage();
                    $operation = array('name' => BOCSpaceMessage::Operation_SendMessage, 'data' => array('message' => 'join to Space request is rejected'));
                    $ocSendMessage->sendOperationRequest($operation, $opReq->sender);
                }
            }
        }
    }

}

?>
